Skip to content

Support i64.mul_wide_s and i64.mul_wide_u from Wide Arithmetic proposal#8652

Open
stevenfontanella wants to merge 1 commit intomainfrom
wide-arithmetic-mul
Open

Support i64.mul_wide_s and i64.mul_wide_u from Wide Arithmetic proposal#8652
stevenfontanella wants to merge 1 commit intomainfrom
wide-arithmetic-mul

Conversation

@stevenfontanella
Copy link
Copy Markdown
Member

@stevenfontanella stevenfontanella commented Apr 28, 2026

Part of #8544. Unit tests were written by Gemini and double-checked against python with this script.

TODO: Move the existing 128 add/sub logic from the interpreter into int128.h.

@stevenfontanella stevenfontanella changed the base branch from main to wide-arithmetic April 28, 2026 17:56
Comment thread src/ir/cost.h Outdated
Base automatically changed from wide-arithmetic to main April 28, 2026 23:57
@stevenfontanella stevenfontanella force-pushed the wide-arithmetic-mul branch 2 times, most recently from eb019af to 64dad92 Compare April 29, 2026 20:05
@stevenfontanella stevenfontanella changed the title [WIP] Wide arithmetic mul Support i64.mul_wide_s and i64.mul_wide_u from Wide Arithmetic proposal Apr 29, 2026
@stevenfontanella stevenfontanella force-pushed the wide-arithmetic-mul branch 6 times, most recently from 3fabe33 to 60966ed Compare April 29, 2026 21:35
@stevenfontanella stevenfontanella marked this pull request as ready for review April 29, 2026 22:59
@stevenfontanella stevenfontanella requested a review from a team as a code owner April 29, 2026 22:59
@stevenfontanella stevenfontanella requested review from tlively and removed request for a team April 29, 2026 22:59
Comment thread src/support/int128.cpp
@@ -0,0 +1,89 @@
// Copyright 2024 WebAssembly Community Group participants
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Copyright 2024 WebAssembly Community Group participants
// Copyright 2026 WebAssembly Community Group participants

Comment thread src/support/int128.h
@@ -0,0 +1,45 @@
// Copyright 2024 WebAssembly Community Group participants
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Copyright 2024 WebAssembly Community Group participants
// Copyright 2026 WebAssembly Community Group participants

Comment thread src/support/int128.cpp
Comment on lines +71 to +73
uint64_t cross = mulLowHigh + (mulLowLow >> 32);
uint64_t carry = cross >> 32;
cross = (cross & 0xffffffff) + mulHighLow;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not immediately obvious to me how cross corresponds to the explanatory comments below. It would be good to make sure the code directly follows from the comments.

It might also be helpful to start with a comment like result = (lhsHigh * 2^32 + lhsLow) * (rhsHigh * 2^32 + rhsLow) = lhsHigh * rhsHigh * 2^64 + lhsHigh * rhsLow * 2^32 + lhsLow * rhsHigh * 2^32 + lhsLow * rhsLow

Comment thread src/support/int128.cpp

namespace detail {

Int128 mul_wide_s_fallback(uint64_t lhs, uint64_t rhs) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have some comments explaining the math here, too.

Comment thread test/gtest/int128.cpp
@@ -0,0 +1,95 @@
// Copyright 2024 WebAssembly Community Group participants
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Copyright 2024 WebAssembly Community Group participants
// Copyright 2026 WebAssembly Community Group participants

Comment thread test/gtest/int128.cpp
Comment on lines +51 to +54
INSTANTIATE_TEST_SUITE_P(Int128,
Int128MulWideSTest,
::testing::Values(mul_wide_s,
detail::mul_wide_s_fallback));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this do? I've never seen TEST_P before.

Comment thread test/gtest/int128.cpp

int64_t maxInt = std::numeric_limits<int64_t>::max();
// Fits in the lower half because the signed bit now goes in the upper half.
EXPECT_EQ(mul_s(maxInt, 2), (Int128{0, 0xfffffffffffffffeULL}));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to show that reversing the order of operands doesn't change the results here and below.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants